import sys
from functools import partial

from PyQt6.QtCore import Qt, QEvent
from PyQt6.QtGui import QAction, QShortcut, QKeySequence
from PyQt6.QtWidgets import QMainWindow, QApplication, QPushButton, QWidget, QSizePolicy

from calculator_init.mainwindow import Ui_MainWindow


class Calculator(Ui_MainWindow, QMainWindow):
    is_equal = False

    def __init__(self):
        super().__init__()
        self.setupUi(self)
        self.setWindowFlags(Qt.WindowType.MSWindowsFixedSizeDialogHint | Qt.WindowType.WindowStaysOnTopHint)
        self.setWindowTitle('Calculator')
        self.grid.setContentsMargins(10, 0, 10, 0);
        self.main.setLayout(self.vbox)
        self.bottom.setLayout(self.grid)


        self.text_btn = [['7', '8', '9', '/'],
                         ['4', '5', '6', '*'],
                         ['1', '2', '3', '-'],
                         ['0', '00', '.', '+'],
                         ['C', 'del', '=', ' ']]
        self.shortcuts = {}

        for e in range(0, len(self.text_btn)):
            for i in range(0, len(self.text_btn[e])):
                if self.text_btn[e][i] == ' ':
                    continue
                btn = QPushButton(self.text_btn[e][i])

                if self.text_btn[e][i] == '=':
                    self.grid.addWidget(btn, e, i, 1, 2)
                else:
                    self.grid.addWidget(btn, e, i)

                btn.setSizePolicy(QSizePolicy.Policy.Expanding, QSizePolicy.Policy.Expanding)

                btn.setObjectName(self.text_btn[e][i])

                btn.clicked.connect(partial(self.buttonFunc,self.text_btn[e][i]))
                shortcut = QShortcut(QKeySequence(self.text_btn[e][i]), self)
                self.shortcuts[self.text_btn[e][i]] = shortcut

                shortcut.activated.connect(partial(self.buttonFunc,self.text_btn[e][i]))

                self.text_btn[e][i] = btn

        self.vbox.setStretchFactor(self.textEdit, 3)
        self.vbox.setStretchFactor(self.bottom, 4)



    def equalOperation(self):
        text = self.textEdit.toPlainText()
        try:
            result = eval(text[:-1])
            self.textEdit.setText(text +" "+ str(result) + '\n')
            self.is_equal = True

        except Exception as e:
            self.textEdit.setText("err" + str(e))

    # 删除符
    def cleanItem(self):
        text = self.textEdit.toPlainText()
        if (len(text) > 0 and self.is_equal == False):
            text = text[:-1]
        self.textEdit.setText(text)

    def cleanAll(self):
        self.textEdit.setText('')

    def buttonFunc(self, str):
        text = self.textEdit.toPlainText()
        if (len(text) > 0):
            self.textEdit.setText(text + " " + str)
        else:
            self.textEdit.setText(str)

        if str == 'del':
            self.cleanItem()
            return
        if str == '=':
            self.equalOperation()
            return
        if str == 'C':
            self.cleanAll()
            return

if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = Calculator()
    window.show()
    sys.exit(app.exec())
